﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SmartSolutions
{
    public partial class ProgressTracker : System.Web.UI.Page
    {
        private Logger oLogger = new Logger(System.Configuration.ConfigurationManager.AppSettings["logger_conf"].ToString());

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (Request.QueryString["Client"] != null)
                {
                    using (SqlConnection sqlConnection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ProgressTracker"].ToString()))
                    {
                        string sSQLQuery = String.Format(@"exec GetClientService @ClientCode='{0}'",
                            Request.QueryString["Client"].Replace("'", "''").Replace("*", "%")
                            );
                        using (SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sSQLQuery, sqlConnection))
                        {
                            using (DataSet oDataSet = new DataSet("ProgressTracker"))
                            {
                                sqlDataAdapter.Fill(oDataSet);

                                oDataSet.Tables[0].TableName = "Service";

                                using (StringWriter sw = new StringWriter())
                                {
                                    oDataSet.WriteXml(sw);
                                    string xmlContent = sw.ToString();
                                    //XmlText.Text = xmlContent.Replace(" ", String.Empty);
                                    XmlText.Text = xmlContent;
                                }
                            }
                        }
                    }
                }
                else if (Request.QueryString["Service"] != null)
                {
                    using (SqlConnection sqlConnection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ProgressTracker"].ToString()))
                    {
                        string sSQLQuery = String.Empty;
                        if (Request.QueryString["StartDate"] != null && Request.QueryString["EndDate"] != null)
                        {
                            sSQLQuery = String.Format(@"exec GetService_ByCodeAndDates @ProjectCode='{0}', @StartDate='{1}', @EndDate='{2}'",
                                Request.QueryString["Service"].Replace("'", "''").Replace("*", "%"),
                                Request.QueryString["StartDate"].Replace("'", "''").Replace("*", "%"),
                                Request.QueryString["EndDate"].Replace("'", "''").Replace("*", "%")
                                );
                        }
                        else
                        {
                            sSQLQuery = String.Format(@"exec GetService_ByCode @ProjectCode='{0}'",
                                Request.QueryString["Service"].Replace("'", "''").Replace("*", "%")
                                );
                        }
                        using (SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sSQLQuery, sqlConnection))
                        {
                            using (DataSet oDataSet = new DataSet("ProgressTracker"))
                            {
                                sqlDataAdapter.Fill(oDataSet);

                                oDataSet.Tables[0].TableName = "Update";

                                using (StringWriter sw = new StringWriter())
                                {
                                    oDataSet.WriteXml(sw);
                                    string xmlContent = sw.ToString();
                                    //XmlText.Text = xmlContent.Replace(" ", String.Empty);
                                    XmlText.Text = xmlContent;
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                this.oLogger.WriteToLog(String.Format("ProgressTacker error occured::{0}", ex.Message), Logger.LogDetails.MsgType.Error);
            }
            finally
            {
                if (this.oLogger != null)
                {
                    this.oLogger.Dispose();
                }
            }
        }
    }
}
